package com.gomore.opple.module.incomeandexpense;

import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.bean.WrapperResponseEntity;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.rest.applybill.ApplyHist;
import com.gomore.opple.rest.applybill.RsApplyBillRequest;
import com.gomore.opple.rest.applybill.RsApplyBillResponse;
import com.gomore.opple.rest.applybill.RsCommissionRequest;
import com.gomore.opple.rest.applybill.RsCommissionResponse;
import com.gomore.opple.rest.common.DataPage;
import com.gomore.opple.rest.consumer.ConsumerPageResult;
import com.gomore.opple.rest.consumer.ConsumerQueryCondition;
import com.gomore.opple.web.cgform.applybill.entity.TOApplyBillEntity;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class IncomeAndExpensePresenter implements IncomeAndExpenseContract.Presenter {

    private DataRepository mDataRepositroy;
    private final IncomeAndExpenseContract.View mView;
    private CompositeSubscription mSubscriptions;
    private int rows = GlobalConstant.PAGE_SIZE;
    private int page = GlobalConstant.PAGE_START;
    private List<ApplyHist> applyHists = new ArrayList<>();
    private List<TOApplyBillEntity> toApplyBillEntities = new ArrayList<>();

    @Inject
    IncomeAndExpensePresenter(DataRepository dataRepository, IncomeAndExpenseContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {

    }

    @Override
    public void queryIncomeAndExpense(final boolean loadMore, final boolean isFirst) {
        if (loadMore) {
            page++;
        } else {
            applyHists.clear();
            page = GlobalConstant.PAGE_START;
        }
        mView.showProgressDialog();
        RsCommissionRequest rsCommissionRequest = new RsCommissionRequest();
        String employeeId = mDataRepositroy.getUser().getEmployee().getId();
        rsCommissionRequest.setEmploreeId(employeeId);
        DataPage dataPage = new DataPage();
        dataPage.setRows(rows);
        dataPage.setPage(page);
        rsCommissionRequest.setPage(dataPage);
        mSubscriptions.clear();
        Subscription subscription = mDataRepositroy.queryIncomeAndExpense(rsCommissionRequest)
                .map(new ServerResponseFunc<RsCommissionResponse>())
                .onErrorResumeNext(new HttpResponseFunc<RsCommissionResponse>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<RsCommissionResponse>() {
                    @Override
                    public void onNext(RsCommissionResponse rsCommissionResponse) {
                        super.onNext(rsCommissionResponse);
                        mView.hideProgressDialog();
                        if (loadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                        if (rsCommissionResponse.getPage() <= rsCommissionResponse.getTotalpages()) {
                            applyHists.addAll(rsCommissionResponse.getList());
                        }
                        mView.showIncomeAndExpense();
                        if (isFirst) {
                            queryApply(false, isFirst);
                        }
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        if (loadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void queryApply(final boolean loadMore, final boolean isFirst) {
        if (loadMore) {
            page++;
        } else {
            toApplyBillEntities.clear();
            page = GlobalConstant.PAGE_START;
        }
        mView.showProgressDialog();
        RsApplyBillRequest rsApplyBillRequest = new RsApplyBillRequest();
        String employeeId = mDataRepositroy.getUser().getEmployee().getId();
        rsApplyBillRequest.setEmployeeId(employeeId);
        DataPage dataPage = new DataPage();
        dataPage.setRows(rows);
        dataPage.setPage(page);
        rsApplyBillRequest.setPage(dataPage);
        mSubscriptions.clear();
        Subscription subscription = mDataRepositroy.queryApply(rsApplyBillRequest)
                .map(new ServerResponseFunc<RsApplyBillResponse>())
                .onErrorResumeNext(new HttpResponseFunc<RsApplyBillResponse>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<RsApplyBillResponse>() {
                    @Override
                    public void onNext(RsApplyBillResponse rsApplyBillResponse) {
                        super.onNext(rsApplyBillResponse);
                        mView.hideProgressDialog();
                        if (loadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                        if (rsApplyBillResponse.getPage() <= rsApplyBillResponse.getTotalpages()) {
                            toApplyBillEntities.addAll(rsApplyBillResponse.getList());
                        }
                        if (!isFirst) {
                            mView.showApply();
                        }
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        if (loadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public List<ApplyHist> getIncomeAndExpenseData() {
        return applyHists;
    }

    @Override
    public List<TOApplyBillEntity> getApplyData() {
        return toApplyBillEntities;
    }

}
